﻿/*
 * MIT License
 *Copyright (c) 2021 易开元(Easy-Open-Meta)

 *Permission is hereby granted, free of charge, to any person obtaining a copy
 *of this software and associated documentation files (the "Software"), to deal
 *in the Software without restriction, including without limitation the rights
 *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *copies of the Software, and to permit persons to whom the Software is
 *furnished to do so, subject to the following conditions:

 *The above copyright notice and this permission notice shall be included in all
 *copies or substantial portions of the Software.

 *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 *SOFTWARE.
 *
 */
using EOM.TSHotelManagement.Common.Contract;
using EOM.TSHotelManagement.Common.Core;
using EOM.TSHotelManagement.Common.Util;
using EOM.TSHotelManagement.EntityFramework;
using jvncorelib.EntityLib;
using SqlSugar;

namespace EOM.TSHotelManagement.Application
{
    /// <summary>
    /// 酒店宣传联动内容接口实现类
    /// </summary>
    public class PromotionContentService : IPromotionContentService
    {
        /// <summary>
        /// 跑马灯
        /// </summary>
        private readonly GenericRepository<PromotionContent> fontsRepository;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fontsRepository"></param>
        public PromotionContentService(GenericRepository<PromotionContent> fontsRepository)
        {
            this.fontsRepository = fontsRepository;
        }

        /// <summary>
        /// 查询所有宣传联动内容
        /// </summary>
        /// <returns></returns>
        public ListOutputDto<ReadPromotionContentOutputDto> SelectPromotionContentAll(ReadPromotionContentInputDto readPromotionContentInputDto)
        {
            var count = 0;
            var where = Expressionable.Create<PromotionContent>();
            if (!readPromotionContentInputDto.IsDelete.IsNullOrEmpty())
            {
                where = where.And(a => a.IsDelete == readPromotionContentInputDto.IsDelete);
            }
            var Data = new List<PromotionContent>();
            if (!readPromotionContentInputDto.IgnorePaging && readPromotionContentInputDto.Page != 0 && readPromotionContentInputDto.PageSize != 0)
            {
                Data = fontsRepository.AsQueryable()
                    .Where(where.ToExpression())
                    .ToPageList(readPromotionContentInputDto.Page, readPromotionContentInputDto.PageSize, ref count);
            }
            else
            {
                Data = fontsRepository.AsQueryable().ToList();
                count = Data.Count;
            }
            var mapped = EntityMapper.MapList<PromotionContent, ReadPromotionContentOutputDto>(Data);
            return new ListOutputDto<ReadPromotionContentOutputDto>
            {
                Data = new PagedData<ReadPromotionContentOutputDto>
                {
                    Items = mapped,
                    TotalCount = count
                }
            };
        }

        /// <summary>
        /// 查询所有宣传联动内容(跑马灯)
        /// </summary>
        /// <returns></returns>
        public ListOutputDto<ReadPromotionContentOutputDto> SelectPromotionContents()
        {
            var Data = fontsRepository.AsQueryable().Where(a => a.IsDelete != 1).ToList();
            var mapped = EntityMapper.MapList<PromotionContent, ReadPromotionContentOutputDto>(Data);
            return new ListOutputDto<ReadPromotionContentOutputDto>
            {
                Data = new PagedData<ReadPromotionContentOutputDto>
                {
                    Items = mapped,
                    TotalCount = mapped.Count
                }
            };
        }

        /// <summary>
        /// 添加宣传联动内容
        /// </summary>
        /// <param name="createPromotionContentInputDto"></param>
        /// <returns></returns>
        public BaseResponse AddPromotionContent(CreatePromotionContentInputDto createPromotionContentInputDto)
        {
            try
            {
                var entity = EntityMapper.Map<CreatePromotionContentInputDto, PromotionContent>(createPromotionContentInputDto);
                var result = fontsRepository.Insert(entity);
                if (!result)
                {
                    LogHelper.LogError(LocalizationHelper.GetLocalizedString("Insert Promotion Content Failed", "宣传联动内容添加失败"), null);
                    return new BaseResponse(BusinessStatusCode.InternalServerError, LocalizationHelper.GetLocalizedString("Insert Promotion Content Failed", "宣传联动内容添加失败"));
                }
            }
            catch (Exception ex)
            {
                LogHelper.LogError(LocalizationHelper.GetLocalizedString("Insert Promotion Content Failed", "宣传联动内容添加失败"), ex);
                return new BaseResponse(BusinessStatusCode.InternalServerError, LocalizationHelper.GetLocalizedString("Insert Promotion Content Failed", "宣传联动内容添加失败"));
            }

            return new BaseResponse(BusinessStatusCode.Success, LocalizationHelper.GetLocalizedString("Insert Promotion Content Success", "宣传联动内容添加成功"));
        }

        /// <summary>
        /// 删除宣传联动内容
        /// </summary>
        /// <param name="deletePromotionContentInputDto"></param>
        /// <returns></returns>
        public BaseResponse DeletePromotionContent(DeletePromotionContentInputDto deletePromotionContentInputDto)
        {
            try
            {
                fontsRepository.SoftDelete(new PromotionContent
                {
                    Id = deletePromotionContentInputDto.Id,
                    PromotionContentNumber = deletePromotionContentInputDto.PromotionContentNumber,
                    IsDelete = deletePromotionContentInputDto.IsDelete,
                    DataChgUsr = deletePromotionContentInputDto.DataChgUsr,
                    DataChgDate = deletePromotionContentInputDto.DataChgDate
                });
            }
            catch (Exception ex)
            {
                LogHelper.LogError(LocalizationHelper.GetLocalizedString("Delete Promotion Content Failed", "宣传联动内容删除失败"), ex);
                return new BaseResponse(BusinessStatusCode.InternalServerError, LocalizationHelper.GetLocalizedString("Delete Promotion Content Failed", "宣传联动内容删除失败"));
            }

            return new BaseResponse(BusinessStatusCode.Success, LocalizationHelper.GetLocalizedString("Delete Promotion Content Success", "宣传联动内容删除成功"));
        }

        /// <summary>
        /// 更新宣传联动内容
        /// </summary>
        /// <param name="updatePromotionContentInputDto"></param>
        /// <returns></returns>
        public BaseResponse UpdatePromotionContent(UpdatePromotionContentInputDto updatePromotionContentInputDto)
        {
            try
            {
                var entity = EntityMapper.Map<UpdatePromotionContentInputDto, PromotionContent>(updatePromotionContentInputDto);
                var result = fontsRepository.Update(entity);
                if (!result)
                {
                    LogHelper.LogError(LocalizationHelper.GetLocalizedString("Update Promotion Content Failed", "宣传联动内容更新失败"), null);
                    return new BaseResponse(BusinessStatusCode.InternalServerError, LocalizationHelper.GetLocalizedString("Update Promotion Content Failed", "宣传联动内容更新失败"));
                }
            }
            catch (Exception ex)
            {
                LogHelper.LogError(LocalizationHelper.GetLocalizedString("Update Promotion Content Failed", "宣传联动内容更新失败"), ex);
                return new BaseResponse(BusinessStatusCode.InternalServerError, LocalizationHelper.GetLocalizedString("Update Promotion Content Failed", "宣传联动内容更新失败"));
            }

            return new BaseResponse(BusinessStatusCode.Success, LocalizationHelper.GetLocalizedString("Update Promotion Content Success", "宣传联动内容更新成功"));

        }
    }
}
